"""
方法一：
class GoodsSerializer(serializers.Serializer):
    name = serializers.CharField(required=True, max_length=100)
    click_num = serializers.IntegerField(default=0)
    goods_front_image = serializers.ImageField()

"""

from rest_framework import serializers
from app.goods.models import GoodsCategory, Goods, GoodsImage, Banner


# ModelSerializer实现商品分类列表页(删除原有的代码)
class CategorySerializer3(serializers.ModelSerializer):
    """三级分类的序列化"""

    class Meta:
        model = GoodsCategory
        fields = "__all__"


class CategorySerializer2(serializers.ModelSerializer):
    """二级分类的序列化"""
    sub_cat = CategorySerializer3(many=True)

    class Meta:
        model = GoodsCategory
        fields = "__all__"


class CategorySerializer(serializers.ModelSerializer):
    """一级分类的序列化"""
    sub_cat = CategorySerializer2(many=True)

    class Meta:
        model = GoodsCategory
        fields = "__all__"  # 指定显示的(返回的)字段。__all__是所有字段
        # exclude = ['goods_desc',]  # 指定不需要序列化显示的字段


class GoodsImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsImage
        # fields = '__all__'
        fields = ('image',)


# ModelSerializer实现商品列表页
class GoodsSerializer(serializers.ModelSerializer):
    # 覆盖外键字段,不指定时，只显示分类的id。 分类信息不仅仅显示分类 id，而是显示分类的详细信息
    category = CategorySerializer()
    # 轮播图的序列化， images是数据库中设置的related_name="images"，把轮播图嵌套进来
    images = GoodsImageSerializer(many=True)

    class Meta:
        model = Goods
        fields = '__all__'


class BannerSerializer(serializers.ModelSerializer):
    '''
    轮播图
    '''
    class Meta:
        model = Banner
        fields = "__all__"
